package com.tansun.easycare.rule.ml.algorithm.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSONObject;
import com.tansun.easycare.core.utils.StringUtils;
import com.tansun.easycare.core.web.BaseController;
import com.tansun.easycare.rule.ml.algorithm.entity.AlogrithmVariables;
import com.tansun.easycare.rule.ml.algorithm.entity.MlWorkflows;
import com.tansun.easycare.rule.ml.algorithm.entity.PyResponse;
import com.tansun.easycare.rule.ml.algorithm.service.IAlgorthmService;
import com.tansun.easycare.rule.ml.model.entity.MlDataModel;
import com.tansun.easycare.rule.ml.model.service.MlDataModelService;

/**
 * 算法工作流程图Controller
 * @author zkx
 * @version 2018-08-22
 */
@Controller
@RequestMapping(value = "/ml/workflows")
public class MlWorkflowsController extends BaseController {

	@Autowired
	private MlDataModelService mlDataModelService;

	@Autowired
	private IAlgorthmService iAlgrthmService;
	

	@ModelAttribute
	public MlDataModel get(@RequestParam(required=false) String id) {
		MlDataModel entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = mlDataModelService.get(id);
		}
		if (entity == null){
			entity = new MlDataModel();
		}
		return entity;
	}
	
	//进入流程图的某一项
	@RequestMapping(value = "goToSite")
	public String goToSite(MlDataModel mlDataModel,String currentSite, RedirectAttributes redirectAttributes, Model model) {
		mlDataModel = mlDataModelService.findByModelId(mlDataModel);
		model.addAttribute("mlDataModel", mlDataModel);
		addMessage(redirectAttributes, "下一页成功");
		String algorithmType = mlDataModel.getAlgorithmType();

		return "rule/ml/"+algorithmType + "/"+ currentSite;
	}
	
	//下一步
	@RequestMapping(value = "nextStep")
	public String nextStep(MlDataModel mlDataModel,String currentSite,RedirectAttributes redirectAttributes, Model model) {
		//改变并获取工作流
		PyResponse wfResp = iAlgrthmService.getWorkflows(mlDataModel,currentSite);
		MlWorkflows workflows= MlWorkflows.newInstance(wfResp.getResponse());
		model.addAttribute("workflows", workflows);
		model.addAttribute("mlDataModel", mlDataModel);
		addMessage(redirectAttributes, "下一步成功");
		String algorithmType = mlDataModel.getAlgorithmType();
		return "rule/ml/"+algorithmType+"/mlDataModelFlowChart";
	}
	
	@RequestMapping(value = "saveModel")
	public @ResponseBody PyResponse saveModel(MlDataModel mlDataModel, RedirectAttributes redirectAttributes) {
		//mlDataModelService.update(mlDataModel);
		PyResponse resp = iAlgrthmService.saveModel(mlDataModel);
		addMessage(redirectAttributes, "模型保存成功");
		return resp;
	}

	@RequestMapping(value = "update")
	public String update(MlDataModel mlDataModel, RedirectAttributes redirectAttributes) {
		//mlDataModelService.update(mlDataModel);
		iAlgrthmService.updateModel(mlDataModel);
		addMessage(redirectAttributes, "更新ml成功");
		return "redirect:"+"/ml/mlDataModel/?repage";
	}
	
	@RequestMapping(value = "dataLoad")
	public  @ResponseBody PyResponse dataLoad(MlDataModel mlDataModel, RedirectAttributes redirectAttributes) {
		PyResponse resp = iAlgrthmService.dataLoad(mlDataModel);
		addMessage(redirectAttributes, "数据导入成功");
		return resp;
	}
	
	@RequestMapping(value = "parameterSetup")
	public @ResponseBody PyResponse parameterSetup(MlDataModel mlDataModel, AlogrithmVariables alogrithmVariables, RedirectAttributes redirectAttributes, Model model) {
		PyResponse resp = iAlgrthmService.parameterSetup(mlDataModel,alogrithmVariables);
		addMessage(redirectAttributes, "变量设置成功");
		return resp;
	}
	
	@RequestMapping(value = "dataSetup")
	public @ResponseBody PyResponse dataSetup(MlDataModel mlDataModel,AlogrithmVariables alogrithmVariables, RedirectAttributes redirectAttributes, Model model) {
		PyResponse resp = iAlgrthmService.dataSetup(mlDataModel,alogrithmVariables);
		addMessage(redirectAttributes, "数据设置成功");
		return resp;
	}
	
	@RequestMapping(value = "dataProcess")
	public @ResponseBody PyResponse dataProcess(MlDataModel mlDataModel, RedirectAttributes redirectAttributes, Model model) {
		PyResponse resp = iAlgrthmService.dataProcess(mlDataModel);
		addMessage(redirectAttributes, "数据处理成功");
		return resp;
	}
	
	@RequestMapping(value = "visualPreview")
	public PyResponse visualPreview(MlDataModel mlDataModel, RedirectAttributes redirectAttributes, Model model) {
		PyResponse resp = iAlgrthmService.visualPreview(mlDataModel);
		addMessage(redirectAttributes, "可视化成功");
		return resp;
	}
	
	@RequestMapping(value = "trainModel")
	public @ResponseBody JSONObject trainModel(MlDataModel mlDataModel, RedirectAttributes redirectAttributes, Model model) {
		PyResponse resp = iAlgrthmService.trainModel(mlDataModel);
		JSONObject result = (JSONObject)resp.getResponse().get("trainModel");
		mlDataModel.setModelStatus("trained");
		mlDataModelService.updateModelStatus(mlDataModel);
		addMessage(redirectAttributes, "模型训练成功");
		return result;
	}
	
}